<?php
namespace app\index\controller;
use think\Controller;

class Order extends Base
{
   public function index(){
	   //dump(input('get.'));
	   $user = $this->getLoginUser();
	   if(!$user){
		   $this->error('请登录', 'user/login');
		   }
	   $id = input('get.id', 0, 'intval');
		if(!$id){
			$this->error('参数不合法');
			}
		
		$dealcount = input('get.deal_count', 1, 'intval');
		$totalprice = input('get.total_price', 1, 'intval');
	   
	   
	   
	   $deal = model('Deal')->find($id);
	   if(!$deal || $deal->status != 1){
			$this->error('商品不存在');
			}
	   if(empty($_SERVER['HTTP_REFERER'])){
		   $this->error('请求不合法');
		   
		   }
		   
		   $orderSn = setOrderSn();
	   $data = [
	       'out_trade_no' => $orderSn,
		   'user_id' => $user->id,
		   'username' => $user->username,
		   'deal_id' => $id,
		   'dealcount' => $dealcount,
		   'total_price' => $totalprice,
		   'referer' => $_SERVER['HTTP_REFERER'],
	   
	   
	   ];
	   //dump($data);exit;
	   try{
	      $orderId = model('Order')->add($data);
	   }catch(\Exception $e){
		   $this->error('订单处理失败');
		   }
	   $this->redirect('pay/index', ['id'=>$orderId]);
	   
	   
	   }
   
    public function confirm()
    {
	   if(!$this->getLoginUser()){
		   $this->error('请登录', 'user/login');
		   }
		//	
		$id = input('get.id', 0, 'intval');
		if(!$id){
			$this->error('参数不合法');
			}
		
		$count = input('get.count', 1, 'intval');
		$deal = model('Deal')->find($id);
		//echo model('Deal')->getLastSql();exit;
		if(!$deal || $deal->status != 1){
			$this->error('商品不存在');
			}
		$deal = $deal->toArray();
		//var_dump($deal);
		
		
		
		
		
		
		return $this->fetch('',[
		'controller' => 'pay',
		'count' => $count,
		'deal' => $deal,
		]);
    }
}










?>